package com.zyit.dao;

import com.zyit.entity.T_base_order;
import com.zyit.entity.T_goods;
import com.zyit.util.BeanRowMapper;
import com.zyit.util.DBHelp;
import com.zyit.util.MapRowMapper;
import com.zyit.util.Page;

import java.util.List;
import java.util.Map;

public class OrderListDao {
    private DBHelp dbHelp = new DBHelp();
    public List<T_goods> findAll() {
        String sql = "select id,goods_name,price,stock,file_name from t_goods";
        return dbHelp.executeQueryForList(sql,new BeanRowMapper<>(T_goods.class));

    }

    public Integer count(){
        String sql = "select count(*) from t_base_order";
        Map<String,Object> map = dbHelp.executeQueryForObject(sql,new MapRowMapper());
        Long count = (Long) map.get("count(*)");
        return count.intValue();
    }

    public Page<T_base_order> findOrderNo(Integer pageNo) {
        Integer count = count();
        Page<T_base_order> page = new Page<>(pageNo,count);
        String sql = "select account,user_address,amount,order_no from t_base_order limit ?,?";
        List<T_base_order> cityList = dbHelp.executeQueryForList(sql,new BeanRowMapper<>(T_base_order.class),page.getStart(),page.getPageSize());
        page.setPageList(cityList);
        return page;
    }

    public List<T_goods> findByPage() {
        String sql = "SELECT t_goods.goods_name,t_goods.price,t_goods.file_name,t_goods.stock,t_base_order.order_no FROM t_goods INNER JOIN t_order_info ON t_goods.id = t_order_info.goods_id INNER JOIN t_base_order ON t_base_order.id = t_order_info.base_order_id";
        return dbHelp.executeQueryForList(sql,new BeanRowMapper<>(T_goods.class));
    }
}
